﻿using wscf.lightblue;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.IO;

namespace wscf.lightblue.Test
{
	[TestClass()]
	public class wscf_exe_Test
	{
		public void DataContractTest()
		{
			string generatedFileName = "wscf_exe_Test.DataContractTest.cs";
			File.Delete(generatedFileName);

			ProcessStartInfo startInfo = new ProcessStartInfo();
			startInfo.FileName = "wscf.exe";
			startInfo.WindowStyle = ProcessWindowStyle.Hidden;
			startInfo.UseShellExecute = false;
			startInfo.RedirectStandardOutput = !startInfo.UseShellExecute;
			startInfo.Arguments = "/m:\"Common*.xsd\" /n:*,Common.Contracts /dconly /o:" + generatedFileName;
			Process p = Process.Start(startInfo);
			p.WaitForExit();
			p.Refresh();

			Assert.AreEqual(0, p.ExitCode, p.StandardOutput.ReadToEnd());
			Assert.IsTrue(File.Exists(generatedFileName));
			string generatedFile = "";
			using (StreamReader sr = new StreamReader(generatedFileName))
			{
				generatedFile = sr.ReadToEnd();
			}
			Assert.IsTrue(generatedFile.Contains("public partial class RequestHeader"));
		}

		public void WildcardTest()
		{
			string generatedFileName = "wscf_exe_Test.RunTest.cs";
			File.Delete(generatedFileName);

			ProcessStartInfo startInfo = new ProcessStartInfo();
			startInfo.FileName = "wscf.exe";
			startInfo.WindowStyle = ProcessWindowStyle.Hidden;
			startInfo.UseShellExecute = false;
			startInfo.RedirectStandardOutput = !startInfo.UseShellExecute;
			startInfo.Arguments = "/m:\"WS_0001_Person v1.wsdl;WS_0001_Person v1.xsd;Common.MessageHeaders v1.xsd;Common.Types v1.xsd\" /n:*,wscf /r:wscf.lightblue.Test.Common.dll /o:" + generatedFileName;
			Process p = Process.Start(startInfo);
			p.WaitForExit();
			p.Refresh();

			Assert.AreEqual(0, p.ExitCode, p.StandardOutput.ReadToEnd());
			Assert.IsTrue(File.Exists(generatedFileName));
			string generatedFile = "";
			using (StreamReader sr = new StreamReader(generatedFileName))
			{
				generatedFile = sr.ReadToEnd();
			}
			Assert.IsTrue(generatedFile.Contains("public interface WS_0001_Person_v1Interface"));
			Assert.IsFalse(generatedFile.Contains("ReplyAction=\"*\""));
			Assert.IsTrue(generatedFile.Contains("public partial class WS_0001_Person_v1InterfaceClient"));
			Assert.IsTrue(generatedFile.Contains("public partial class FindPersonRequest"));
			Assert.IsFalse(generatedFile.Contains("public partial class RequestHeader"));
		}

		public void FileListTest()
		{
			string generatedFileName = "wscf_exe_Test.RunTest.cs";
			File.Delete(generatedFileName);

			ProcessStartInfo startInfo = new ProcessStartInfo();
			startInfo.FileName = "wscf.exe";
			startInfo.WindowStyle = ProcessWindowStyle.Hidden;
			startInfo.UseShellExecute = false;
			startInfo.RedirectStandardOutput = !startInfo.UseShellExecute;
			startInfo.Arguments = "/m:\"WS_0001_Person v1*\" /n:*,wscf /r:wscf.lightblue.Test.Common.dll /o:" + generatedFileName;
			Process p = Process.Start(startInfo);
			p.WaitForExit();
			p.Refresh();

			Assert.AreEqual(0, p.ExitCode, p.StandardOutput.ReadToEnd());
			Assert.IsTrue(File.Exists(generatedFileName));
			string generatedFile = "";
			using (StreamReader sr = new StreamReader(generatedFileName))
			{
				generatedFile = sr.ReadToEnd();
			}
			Assert.IsTrue(generatedFile.Contains("public interface WS_0001_Person_v1Interface"));
			Assert.IsFalse(generatedFile.Contains("ReplyAction=\"*\""));
			Assert.IsTrue(generatedFile.Contains("public partial class WS_0001_Person_v1InterfaceClient"));
			Assert.IsTrue(generatedFile.Contains("public partial class FindPersonRequest"));
			Assert.IsFalse(generatedFile.Contains("public partial class RequestHeader"));
		}

	}
}
